package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesBasicReportWriteRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportSDO;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.BatchCreateRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.DeleteRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.DeleteResponse;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.UpdateMonthPlanDataResponse;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.UpdateResponse;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.dto.UpdateSalesOrderCompleteDataResponse;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.model.MonthSalesBasicReport;
import com.epoch.app.otsapplicationreportsdk.monthsalesbasicreport.service.MonthSalesBasicReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/11/11 22:19
 */
@Component
public class MonthSalesBasicReportWriteRepositoryImpl implements MonthSalesBasicReportWriteRepository {

    Log log = Log.getLogger(MonthSalesBasicReportWriteRepositoryImpl.class);

    @Resource
    MonthSalesBasicReportService monthSalesBasicReportService;

    @Override
    public String create(MonthSalesBasicReportSDO sdo) {
        MonthSalesBasicReport monthSalesBasicReport = new MonthSalesBasicReport();
        BeanUtils.copyProperties(sdo,monthSalesBasicReport);
        // 转化精度
        setSdoScale(monthSalesBasicReport);
        Map m = (Map)monthSalesBasicReportService.create(monthSalesBasicReport);
        Map result = (Map)m.get("result");
        return (String)result.get("lastInsertId");
    }

    /**
     * 转化精度
     * @param monthSalesBasicReport 入参
     */
    private void setSdoScale(MonthSalesBasicReport monthSalesBasicReport) {
        monthSalesBasicReport.setBeforeMonthMoneyComplete(monthSalesBasicReport.getBeforeMonthMoneyComplete().setScale(2, RoundingMode.HALF_UP));
    }

    @Override
    public Integer update(MonthSalesBasicReportSDO sdo) {
        MonthSalesBasicReport monthSalesBasicReport = new MonthSalesBasicReport();
        BeanUtils.copyProperties(sdo,monthSalesBasicReport);
        UpdateResponse response = monthSalesBasicReportService.update(monthSalesBasicReport);
        return response.getCount();
    }

    @Override
    public Integer batchDelete(List<Long> ids) {
        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.setIds(ids);
        DeleteResponse response = monthSalesBasicReportService.delete(deleteRequest);
        return response.getCount();
    }

    @Override
    public Integer updateMonthPlanData(MonthSalesBasicReportSDO sdo) {
        MonthSalesBasicReport monthSalesBasicReport = new MonthSalesBasicReport();
        BeanUtils.copyProperties(sdo,monthSalesBasicReport);
        UpdateMonthPlanDataResponse response = monthSalesBasicReportService.updateMonthPlanData(monthSalesBasicReport);
        return response.getCount();
    }

    @Override
    public Integer updateSalesOrderCompleteData(MonthSalesBasicReportSDO sdo) {
        MonthSalesBasicReport monthSalesBasicReport = new MonthSalesBasicReport();
        BeanUtils.copyProperties(sdo,monthSalesBasicReport);
        UpdateSalesOrderCompleteDataResponse response = monthSalesBasicReportService.updateSalesOrderCompleteData(monthSalesBasicReport);
        return response.getCount();
    }

    @Override
    public void batchCreate(List<MonthSalesBasicReportSDO> sdoList){
        List<MonthSalesBasicReport> reportList = new ArrayList<>();
        int batch = 0;
        for(MonthSalesBasicReportSDO sdo:sdoList){
            batch++;
            MonthSalesBasicReport basic = new MonthSalesBasicReport();
            BeanUtils.copyProperties(sdo,basic);
            reportList.add(basic);
            if (batch%500==0){
                BatchCreateRequest request = new BatchCreateRequest();
                request.setReportList(reportList);
                Object response = monthSalesBasicReportService.batchCreate(request);
                log.info("monthSalesBasicReportService_batchCreate_reportList_size={},response={}", reportList.size(), JSON.toJSONString(response));
                reportList.clear();
            }
        }
        if (reportList.size()>0){
            BatchCreateRequest request = new BatchCreateRequest();
            request.setReportList(reportList);
            Object response = monthSalesBasicReportService.batchCreate(request);
            log.info("monthSalesBasicReportService_batchCreate2_reportList2_size={},response2={}", reportList.size(), JSON.toJSONString(response));
        }
    }
}
